/*
* @Author: 严涛
* @Date:   2018-07-05 16:17:20
* @Last Modified by:   'happytaohaha'
* @Last Modified time: 2018-07-06 08:05:25
*/
var webpack 		= require('webpack');
var ExtractTextPlugin 				= require('extract-text-webpack-plugin');
const path 			= require('path');
var htmlWebpackPlugin = require('html-webpack-plugin');

// 环境变量配置，dev / online
var WEBPACK_ENV         = process.env.WEBPACK_ENV || 'dev';
// 获取html-webpack-plugin参数的方法 
var getHtmlConfig = function(name, title){
    return {
        template    : './src/view/' + name + '.html',
        filename    : 'view/' + name + '.html',
        favicon     : './favicon.ico',
        title       : title,
        inject      : true,
        hash        : true,
        chunks      : ['common', name]
    };
};
var config  = {
	entry : {//输入文件
		'common': ['./src/page/common/index.js'],
		'index' : ['./src/page/index/index.js'],
		'login' : ['./src/page/login/login.js'],
	},
	output : {//输出文件
		//path: path.resolve(__dirname, 'dist'),
		path        : __dirname+'/dist/',//生成文件的路径
		//除域名外的扩展名，配置这个就会使他只要你更改他就会自动刷新
		publicPath  : '/dist/',
		filename:'js/[name].js'//这个是输出相对应的文件
	},
	externals : {//外部的引入
		'jquery':'window.jQuery'
	},
	module: {
        rules: [
        {	test: /\.css$/, use: ExtractTextPlugin.extract("css-loader", "style-loader")},
        //小于100的会以base64的形式存储 ，要不然以文件的形式存储
        { 	test: /\.(gif|png|jpg|woff|svg|eot|ttf)\??.*$/, use: 'url-loader?limit=100&name=/resource/[name].[ext]' },
        { 
                test: /\.string$/,
                loader: 'html-loader',
                query:{
                    minimize: true,
                    removeAttributeQuotes :false
                }
            }
        ]
    },
    resolve : {
        alias : {
            node_modules    : __dirname + '/node_modules',
            util            : __dirname + '/src/util',
            page            : __dirname + '/src/page',
            service         : __dirname + '/src/service',
            image           : __dirname + '/src/image'
        }
    },
  	plugins : [
  		
  		//css单独打包
  		new ExtractTextPlugin("css/[name].css"),
  		//html模板处理
  		new htmlWebpackPlugin(getHtmlConfig('index')),
  		new htmlWebpackPlugin(getHtmlConfig('login')),
  	],
	optimization: {//通用模块的配置  ,这个没法放到plugins 里
	    splitChunks: {
		        cacheGroups: {
		            commons: {
		                test: /[\\/]node_modules[\\/]/,
		                //通用模块 ,上边的输入common 这两个名字一样
		                name: "common",
		                chunks: "all"
		            }
		        }
	    }
	}
 };
if('dev' === WEBPACK_ENV){
    config.entry.common.push('webpack-dev-server/client?http://localhost:8086/');
}

module.exports = config;